package com.efounder.frame.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.efounder.frame.activity.EFTransformFragmentActivity;
import com.efounder.frame.exception.NotActivityOrFragmentException;

import java.lang.ref.WeakReference;

/**
 * Created by hudq on 2016/8/25.
 */

public class EFFrameUtils {

    /**
     * 当前Activity
     **/
    private static WeakReference<Activity> currentActivityWeakReference;

    public static Activity getCurrentActivity() {
        Activity activity = null;
        if (currentActivityWeakReference != null) {
            activity = currentActivityWeakReference.get();
        }
        return activity;
    }

    public static void setCurrentActivity(Activity currentActivity) {
        currentActivityWeakReference = new WeakReference<Activity>(currentActivity);
    }

    /***
     * @param activityOrFragmentClass
     * @param bundle
     */
    public static void push(Class activityOrFragmentClass, @Nullable Bundle bundle) {
        if (Activity.class.isAssignableFrom(activityOrFragmentClass)) {
            pushActivity(activityOrFragmentClass, bundle);
        } else if (Fragment.class.isAssignableFrom(activityOrFragmentClass)) {
            pushFragment(activityOrFragmentClass, bundle);
        } else {
            throw new NotActivityOrFragmentException("传入的class不是Activity或android.support.v4.app.Fragment");
        }
    }

    public static void pushActivity(Class<? extends Activity> activityClass, @Nullable Bundle bundle
            , int inTransition, int outTransition) {
        startActivity(activityClass, bundle, inTransition, outTransition);
    }

    public static void pushActivity(Class<? extends Activity> activityClass, @Nullable Bundle bundle) {
        startActivity(activityClass, bundle);
    }

    public static void pushFragment(Class<? extends Fragment> fragmentClass, @Nullable Bundle bundle) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putString(EFTransformFragmentActivity.EXTRA_FRAGMENT_NAME, fragmentClass.getName());
        startActivity(EFTransformFragmentActivity.class, bundle);
    }

    public static void pushFragment(Class<? extends Fragment> fragmentClass, @Nullable Bundle bundle,
                                    int inTransition, int outTransition) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putString(EFTransformFragmentActivity.EXTRA_FRAGMENT_NAME, fragmentClass.getName());
        startActivity(EFTransformFragmentActivity.class, bundle, inTransition, outTransition);
    }

    private static void startActivity(Class<? extends Activity> activityClass, @Nullable Bundle bundle) {
        Intent intent = new Intent(getCurrentActivity(), activityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        getCurrentActivity().startActivity(intent);
    }

    public static void startActivity(Class<? extends Activity> activityClass, @Nullable Bundle bundle,
                                     int inTransition, int outTransition) {
       startActivity(activityClass,bundle,inTransition,outTransition,false);
    }

    public static void startActivity(Class<? extends Activity> activityClass, @Nullable Bundle bundle,
                                     int inTransition, int outTransition, boolean forResult) {
        Intent intent = new Intent(getCurrentActivity(), activityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        if (forResult) {
            getCurrentActivity().startActivityForResult(intent, 20);
        } else {
            getCurrentActivity().startActivity(intent);
        }

        if ((inTransition != 0) && (outTransition != 0)) {
            getCurrentActivity().overridePendingTransition(inTransition, outTransition);
        }
    }

    public static void pop() {

    }

}
